package com.yxe.controller;



import cn.dev33.satoken.stp.StpUtil;
import com.yxe.api.CommonResult;
import com.yxe.dto.ContractImage;
import com.yxe.pojo.Contract;
import com.yxe.service.ContractService;
import com.yxe.utils.AliyunOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangl
 * @since 2022-12-08
 */
@RestController
@RequestMapping("contract")
@Slf4j
@Api(tags = "合同模块")
public class ContractController {

    @Autowired
    private ContractService contractService;

    @Autowired
    private AliyunOssUtil ossUtil;

    @PostMapping("addContract")
    @ApiOperation("上传合同")
    @ResponseBody
    public CommonResult addContract(@RequestParam("name") String name,
                                    @RequestParam("identificationCard") String identificationCard,
                                    @RequestParam("contractImageUp") MultipartFile contractImageUp,
                                    @RequestParam("contractImageDown") MultipartFile contractImageDown) {



        if (name.length() >= 2 && (identificationCard.length() == 18)){

            log.info("文件一：" + contractImageUp.getOriginalFilename());
            log.info("文件二：" + contractImageDown.getOriginalFilename());

            // 将文件添加到集合中
            List<MultipartFile> fileList = new ArrayList<>(2);
            fileList.add(contractImageUp);
            fileList.add(contractImageDown);

            List<String> stringList = new ArrayList<>();

            try {
                for (MultipartFile file : fileList) {
                    if (file != null){
                        String filename = file.getOriginalFilename();

                        if (!filename.trim().equals("")){
                            File newFile = new File(filename);
                            FileOutputStream os = new FileOutputStream(newFile);
                            os.write(file.getBytes());
                            os.close();

                            //把file里的内容复制到newFile中
                            file.transferTo(newFile);
                            String upload = ossUtil.upload(newFile);

                            // 拼接产生完整地址
                            String newUpload = "https://baiyang425.oss-cn-hangzhou.aliyuncs.com/".concat(upload);

                            //图片回显地址:
                            log.info("图片地址" + newUpload);

                            stringList.add(newUpload);
                        }

                    }
                }

                Contract contract = new Contract();
                contract.setName(name);
                contract.setIdentificationCard(identificationCard);
                contract.setContractImageUp(stringList.get(0));
                contract.setContractImageDown(stringList.get(1));
                contract.setUserId(StpUtil.getLoginIdAsInt());

                // 添加合同信息到数据库中
                contractService.addContract(contract);

                return CommonResult.success("上传成功");


            } catch (IOException e) {
                e.printStackTrace();
            }

        }

        return CommonResult.failed("合同上传失败，请重新尝试");

    }



    @GetMapping("lookContract")
    @ApiOperation("查看合同")
    public CommonResult<ArrayList<String>> lookContract(){

        Integer userId = StpUtil.getLoginIdAsInt();

        ContractImage contractImage = contractService.lookContract(userId);

        // 俩张合同的路径
        log.info("合同地址：" + contractImage);

        ArrayList<String> arrayList = new ArrayList<>();

        arrayList.add(contractImage.getContractImageUp());
        arrayList.add(contractImage.getContractImageDown());

        return CommonResult.success(arrayList,"查看成功");

//      return   contractImage.getContractImageUp();

    }
}

